package com.mall.common.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Order;
import com.mall.common.bean.Pager;
import com.mall.common.domain.content.AdvertPositionDomain;
import com.mall.common.exception.NoticeException;
import com.mall.common.service.AdvertPositionService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;

/**
 * 后台Action类 - 广告位
 */
@Controller
@RequestMapping("/admin/system/adPosition")
public class AdvertPositionController extends BaseController {

	@Resource private AdvertPositionService advertPositionService;

	/**
	 * 跳转到列表页面
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		return "/admin/system/adPosition/list";
	}

	//获取列表
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager){
		pager.addOrder(Order.asc("id"));
    	pager = advertPositionService.findPage(pager);
		return JsonDataGridHelper.createJSONData(pager);
    }
    
	/**
	 * 校验广告位置是否重复
	 * @return
	 */
	@RequestMapping(value="/checkTitle", method=RequestMethod.POST)
	@ResponseBody
	public String checkTitle(AdvertPositionDomain advertPosition, String oldValue){
		String newValue = advertPosition.getTitle();
		if (advertPositionService.isUnique("title", oldValue, newValue)) {
			return "true";
		} else {
			return "false";
		}
    }
    
    /**
	 * 校验广告位置是否重复
	 * @return
	 */
	@RequestMapping(value="/checkPosition", method=RequestMethod.POST)
	@ResponseBody
	public String checkPosition(AdvertPositionDomain advertPosition, String oldValue){
		String newValue = advertPosition.getPosition();
		if (advertPositionService.isUnique("position", oldValue, newValue)) {
			return "true";
		} else {
			return "false";
		}
    }
	
	//编辑
    @Validations(
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "广告位不存在!")
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit(Long id, Model model){
    	AdvertPositionDomain advertPosition = advertPositionService.find(id);
    	if(advertPosition == null ){
    		throw new NoticeException("广告位不存在");
    	}
    	model.addAttribute("advertPosition", advertPosition);
		return "/admin/system/adPosition/input";
	}
	
	/**
	 * 修改广告位
	 * @return
	 */
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "title", message = "标题不允许为空!"),
			@RequiredStringValidator(fieldName = "position", message = "位置名称不允许为空!")			
		},
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "广告位置不存在!"),
			@RequiredFieldValidator(fieldName = "width", message = "宽度不允许为空!"),
			@RequiredFieldValidator(fieldName = "height", message = "高度不允许为空!")
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(AdvertPositionDomain advertPosition, HttpServletResponse response){
    	AdvertPositionDomain current = advertPositionService.find(advertPosition.getId());
    	if(current == null){
    		throw new NoticeException("广告位不存在");
    	}
		if(!advertPositionService.isUnique("title", current.getTitle(), advertPosition.getTitle())){
			throw new NoticeException("广告位置标题重复");
		}
		if(!advertPositionService.isUnique("position", current.getTitle(), advertPosition.getTitle())){
			throw new NoticeException("广告位置重复");
		}
		advertPositionService.updateByUpdater(advertPosition);
		return ResponseUtils.renderJSON(successMessage, response);
	}
    
    /**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String input(Model model){
		return "/admin/system/adPosition/input";
	}
	
	/**
	 * 保存广告位
	 * @return
	 */
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "title", message = "标题不允许为空!"),
			@RequiredStringValidator(fieldName = "position", message = "位置名称不允许为空!")			
		},
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "width", message = "宽度不允许为空!"),
			@RequiredFieldValidator(fieldName = "height", message = "高度不允许为空!")
		}
	)
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(AdvertPositionDomain advertPosition, HttpServletResponse response){
		if(!advertPositionService.isUnique("title", null, advertPosition.getTitle())){
			throw new NoticeException("广告位置标题重复");
		}
		if(!advertPositionService.isUnique("position", null, advertPosition.getPosition())){
			throw new NoticeException("广告位置重复");
		}
		advertPositionService.persist(advertPosition);
		return ResponseUtils.renderJSON(successMessage, response);
    }
    
	
    /**
	 * 删除广告位
	 * @return
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "ids非法")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response){
    	advertPositionService.remove(ids);
    	return ResponseUtils.renderJSON(successMessage, response);
    }
}
